package com.sxbbc.api.controller;


import com.alibaba.fastjson.JSONArray;
import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.dto.jz.AddressVo;
import com.sxbbc.common.core.mapper.jz.AddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.api.common.MappingUtils;
import com.sxbbc.api.service.ISystemService;
import com.sxbbc.common.core.entity.jz.SysEssay;
import com.sxbbc.common.core.service.ISysEssayService;
import com.sxbbc.common.core.utils.BasePage;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController

public class SystemController {

	@Autowired
	private ISystemService systemService;
	@Autowired
	private ISysEssayService sysEssayService;
	@Autowired
	private AddressMapper addressMapper;
	
	/**
	 * 查询商学院数据
	 * @param page 分页
	 * @param essay 查询数据
	 * @return
	 */
	@RequestMapping(value=MappingUtils.QUERY_ESSAY_LIST)
	public RestResponse queryEssayList(BasePage<SysEssay> page, SysEssay essay) {
		IPage<SysEssay> pageInfo = sysEssayService.queryEssay(page, essay);
		return GetRest.getSuccess("", pageInfo);
	}
	
	/**
	 * 商学院详情数据
	 * @param essayId 主键
	 * @return
	 */
	@RequestMapping(value=MappingUtils.GET_ESSAY_DETAIL)
	public RestResponse getEssayDetail (Integer essayId) {
		SysEssay essay = sysEssayService.getById(essayId);
		return GetRest.getSuccess("", essay);
	}
	
	/**
	 * 新闻公告详情
	 * @param id 新闻公告id
	 * @return
	 * @author xiaoky
	 */
	@RequestMapping(value=MappingUtils.GET_NEWS_DETAIL)
	public RestResponse getSysNewsById(Integer id) {
		return  GetRest.getSuccess("", systemService.getSysNewsById(id));
	}

	/**
	 * 获取地址生成json文件
	 * @param request
	 * @return
	 * @author xiaoky
	 */
	@RequestMapping(value = "/createAddrJson")
	@ResponseBody
	public JSONArray createAddrJson (HttpServletRequest request) {
		String filePath = request.getSession().getServletContext().getRealPath("");
		List<AddressVo> proList = addressMapper.queryAddrByParentId(StaticUtils.ADDRESS_COUNTRY);
		for (AddressVo pro : proList) {
			List<AddressVo> cityList = addressMapper.queryAddrByParentId(pro.getId());// 该省的市
			for(AddressVo city:cityList){
				List<AddressVo> areaList = addressMapper.queryAddrByParentId(city.getId());// 该市的区域
				city.setList(areaList);
			}
			pro.setList(cityList);
		}
		JSONArray array= JSONArray.parseArray(JSONArray.toJSONString(proList));
		CalcuUtils.getInstance().createAddrJson(filePath, array);
		return array;
	}
}
